//加载信息
function initHint() {
	for(var field in hint) {
		$("#" + field).tooltip({
			title: hint[field],
			placement: "top"
		});
	}
}

initHint();

//更改主C伤害类型
$("#harm_type a").click(function() {
	nurse.harm_type.name = $(this).text();
	nurse.harm_type.powerid = $(this).data("powerid");
	nurse.harm_type.attackid = $(this).data("attackid");
	nurse.harm_type.power = $(this).data("power");
	nurse.harm_type.attack = $(this).data("attack");
	$("#c_power").attr("placeholder", "输入" + nurse.harm_type.power);
	$("#c_attack").attr("placeholder", "输入" + nurse.harm_type.attack);
	$("#harm_type button").text(nurse.harm_type.name);
	show();
})
//奶妈更改智力类型
$("#brains_type a").click(function() {
	nurse.brains_type = $(this).text();
	$("#n_brains").attr("placeholder", "请输入" + $(this).text());
	if(nurse.brains_type == "适用智力") {
		nurse.n_faith = 0;
		$("#n_faith").attr("disabled", true);
	} else {
		nurse.n_faith = parseInt($("#n_faith").val());
		$("#n_faith").attr("disabled", false);
	}
	$("#brains_type button").text(nurse.brains_type);
	arithmetic();
})

$("#oracle_brains_type a").click(function() {
	nurse.brains_type = $(this).text();
	$("#oracle_brains").attr("placeholder", "请输入" + $(this).text());
	if(nurse.brains_type == "适用智力") {
		nurse.oracle_faith = 0;
		$("#oracle_faith").attr("disabled", true);
	} else {
		nurse.oracle_faith = parseInt($("#oracle_faith").val());
		$("#oracle_faith").attr("disabled", false);
	}
	$("#oracle_brains_type button").text(nurse.oracle_brains_type);
})

//改变时
$(".form-control").change(edit);

$("#n_seven input").change(function() {
	var i = $(this).val();
	var boo = $(this).prop("checked");
	if(boo) {
		nurse.n_seven[i] = 1;
	} else {
		nurse.n_seven[i] = 0;
	}

	extra();
	arithmetic();
});

//特殊装备计算
function extra() {
	//初始化数据
	perData = {
		power: [0, 0], //力量奶量
		physical_att: [0, 0], //物攻奶量
		brains: [0, 0], //智力奶量
		magic_att: [0, 0], //魔攻奶量
		fixed_att: [0, 0], //独立
	}

	/*计算七宗罪额外属性start*/
	for(var i = 0; i < 3; i++) {
		if(nurse.n_seven[i] == 1) {
			for(var k in sevenData.one[i]) {
				perData[k][0] += sevenData.one[i][k][0];
				perData[k][1] += sevenData.one[i][k][1];
			}
		}

	}
	//计算七宗罪三额外属性
	var n = 0;
	$(nurse.n_seven).each(function(i) {
		if(nurse.n_seven[i] == 1) {
			n++;
		}
	})
	if(n >= 3) {
		for(var k in sevenData.series) {
			perData[k][0] += sevenData.series[k][0];
			perData[k][1] += sevenData.series[k][1];
		}
	}
	/*计算七宗罪额外属性end*/
	/**
	 * 武器额外属性
	 */
	for(var k in weaponData[nurse.n_weapon]) {
		perData[k][0] += weaponData[nurse.n_weapon][k][0];
		perData[k][1] += weaponData[nurse.n_weapon][k][1];
	}

}

//编辑方法
function edit() {
	var id = $(this).attr("id");
	var text = $(this).val();
	if(text == "") {
		nurse[id] = 0;
		return;
	}
	nurse[id] = parseInt(text);

	//计算武器额外属性
	if(id == "n_weapon") {
		extra();
	}

	//计算
	arithmetic();

}

//算法
function arithmetic() {
	//计算勇气
	if(nurse.n_brains) {
		countCourage();
	}
	//计算一觉
	if(nurse.oracle_brains) {
		countOracle();
	}

	//计算勇气+一觉
	if(nurse.n_brains && nurse.oracle_brains) {
		countAll();
	}

	//计算主C提升率

	//显示
	show();

}

//勇气算法
function countCourage() {
	//勇气祝福等级
	var courageNum = nurse.n_courage;
	//智力
	var brainsNum = nurse.n_brains;
	//虔诚信念等级
	var faithNum = nurse.n_faith;

	//加成一觉被动智力值
	if(faithNum > 0) {
		brainsNum += faithData[faithNum];
	}

	//智力系数
	var brains_coeff = brainsNum / 700 + 1;
	//计算勇气+圣歌
	var ca = 1.25 + (nurse.n_seven[4] == 1 ? 0.05 : 0);
	//遍历勇气祝福加成字段
	for(var td in result.courage) {
		//勇气祝福技能面板
		var courage_coeff = courageData[td][courageNum];
		//技能面板*额外百分比提升 + 固定提升
		courage_coeff = courage_coeff * (1 + perData[td][0]) + perData[td][1];
		//实际面板=技能面板*智力系数
		var sum = courage_coeff * brains_coeff;
		result.courage[td] = sum;
		result.courage_anthem[td] = sum * ca;
	}

}
//	power: 0, //力量奶量
//	physical_att: 0, //物攻奶量
//	brains: 0, //智力奶量
//	magic_att: 0, //魔攻奶量
//	fixed_att: 0, //独立

//一觉算法
function countOracle() {

	//	n_oracle: 20, //奶一觉
	//	oracle_brains_type: '站街智力', //一觉智力类型
	//	oracle_brains: 0, //一觉智力
	//	oracle_faith: 0, //一觉时一绝被动

	//一觉等级
	var oracleNum = nurse.n_oracle;
	//智力
	var brainsNum = nurse.oracle_brains;
	//虔诚信念等级
	var faithNum = nurse.oracle_faith;

	//加成一觉被动智力值
	if(faithNum > 0) {
		brainsNum += faithData[faithNum];
	}

	//智力系数
	var brains_coeff = brainsNum / 830 + 1;
	//一觉技能面板
	var courage_coeff = oracleData[oracleNum];
	//实际面板=技能面板*智力系数
	var sum = courage_coeff * brains_coeff;
	//圣歌
	result.oracle.power = sum;
	result.oracle.brains = sum;

}

function countAll() {
	//遍历勇气祝福和一觉加成字段
	for(var td in result.courage) {
		result.courage_oracle[td] = result.courage_anthem[td] + result.oracle[td];
	}
}

//四舍五入
function qxs(num) {
	return parseInt(num.toFixed(0));
}

//显示
function show() {
	var body = $("#result_body");
	for(var k in result) {
		var data = result[k];
		var tr = body.find("#" + k);
		for(var c in data) {
			var num = qxs(data[c]);
			tr.find("." + c).text(num)
		}
	}
	//更新图表
	var powerid = nurse.harm_type.powerid;
	var attackid = nurse.harm_type.attackid;

	var c_power = qxs(nurse.c_power);
	var c_attack = nurse.c_attack;
	c_attack = attackid == 'fixed_att' ? c_attack : c_attack / (c_power / 250 + 1);
	c_attack = qxs(c_attack);
	var boo = attackid == 'fixed_att' | c_power != 0;

	//攻击
	var a1 = result.courage[attackid] != 0 && c_attack != 0 && boo ? c_attack + result.courage[attackid] * (1 + nurse.c_attackb / 100) : 0;
	a1 = qxs(a1);
	var a2 = result.courage_oracle[attackid] != 0 && c_attack != 0 && boo ? c_attack + result.courage_oracle[attackid] * (1 + nurse.c_attackb / 100) : 0;
	a2 = qxs(a2)
	//力量智力
	var b1 = result.courage[powerid] != 0 && c_power != 0 ? c_power + (result.courage[powerid] * (1 + nurse.c_powerb / 100)) : 0;
	b1 = qxs(b1);
	var b2 = result.courage_oracle[powerid] != 0 && c_power != 0 ? c_power + (result.courage_oracle[powerid] * (1 + nurse.c_powerb / 100)) : 0;
	b2 = qxs(b2);
	chart.update({
		series: [{
			name: nurse.harm_type.attack,
			data: [boo ? c_attack : 0, a1, a2]
		}, {
			name: nurse.harm_type.power,
			data: [c_power, b1, b2]

		}]
	});

}

